package cn.intotw.pp.util;

import cn.intotw.pp.constants.Constant;

import java.io.UnsupportedEncodingException;

/**
 * @author Chenxiang
 * @Description: 字节工具类
 * @create 2018/8/20 16:40
 */
public class ByteUtil {
    public static int bytesToInt(byte[] bytes,int startIndex)
    {
        return   bytes[startIndex] & 0xFF |
                (bytes[startIndex++] & 0xFF) << 8 |
                (bytes[startIndex++] & 0xFF) << 16 |
                (bytes[startIndex++] & 0xFF) << 24;
    }
    public static int bytesToLong(byte[] bytes,int startIndex)
    {
        return   bytes[startIndex] & 0xFF |
                (bytes[startIndex++] & 0xFF) << 8 |
                (bytes[startIndex++] & 0xFF) << 16 |
                (bytes[startIndex++] & 0xFF) << 24 |
                (bytes[startIndex++] & 0xFF) << 32 |
                (bytes[startIndex++] & 0xFF) << 40 |
                (bytes[startIndex++] & 0xFF) << 48 |
                (bytes[startIndex++] & 0xFF) << 56 ;
    }
    public static String bytesToString(byte[] bytes,int startIndex) throws UnsupportedEncodingException {
        return new String(bytes,startIndex,bytes.length-startIndex, Constant.CHARSET_UTF8);
    }
}